package com.xueqiu.movieplaymanagersystem.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.xueqiu.movieplaymanagersystem.entity.User;
import com.xueqiu.movieplaymanagersystem.mapper.UserMapper;
import com.xueqiu.movieplaymanagersystem.service.UserService;

/**
 * @author xueqiu
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param user 数据源；包含用户名和密码
     * @return 用户详细信息；剔除了密码
     */
    @Override
    @Transactional(readOnly = true)
    public User login(User user) {
        //先执行查询
        User originResult = userMapper.selectByName(user.getUsername());

        //未找到直接返回空
        if (originResult == null) {
            return null;
        }
        //找到则判断是否与参数中的密码一致
        //TODO 数据库中是明文密码；项目没有新增用户业务
        if (originResult.getPassword().equals(user.getPassword())) {
            originResult.setPassword(null);
        } else {
            originResult = null;
        }
        log.info("UserServiceImpl login originResult ----> {}",originResult);
        return originResult;
    }

    /**
     * 保持user在域中存活
     * @return 目标user
     */
    @Override
    @Transactional(readOnly = true)
    public User findIndexUserInfo() {
        return userMapper.findIndexUserInfo();
    }
}
